package com.life.wd.controller;

import com.life.wd.constant.MessageConstant;
import com.life.wd.dto.EmployeeDTO;
import com.life.wd.dto.EmployeeLoginDTO;
import com.life.wd.entity.Employee;
import com.life.wd.result.Result;
import com.life.wd.service.EmployeeService;
import com.life.wd.utils.JwtUtil;
import com.life.wd.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/admin/emp")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/login")
    public Result login(@RequestBody EmployeeLoginDTO employeeLoginDTO) throws Exception {
        log.info("员工登录：{}",employeeLoginDTO);
        Employee emp = employeeService.loginService(employeeLoginDTO);
        if(emp == null) return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("EmpId",emp.getId());
        String token = JwtUtil.GenerateJwt(claims);
        EmployeeVO empVO = EmployeeVO.builder()
                .id(emp.getId())
                .name(emp.getName())
                .userName(emp.getUsername())
                .token(token)
                .build();
        return Result.success(empVO);
    }

    @PostMapping
    public Result insert(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}",employeeDTO);
        employeeService.insert(employeeDTO);
        return Result.success(MessageConstant.INSERT_EMP_SUCCESS);
    }
}
